﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BFOAuction.BLL.Config;

namespace BFOAuction.Config
{
    public partial class wfrmConfig : System.Web.UI.Page
    {
        #region Khai báo biến
        AppFormat.MyError objError = null;

        #endregion

        #region Các hàm sự kiện
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Update();
        }

        #endregion

        #region Các hàm hổ trợ

        private void LoadData()
        {
            objError = new AppFormat.MyError();
            DTO.Config.Config objConfig=new DTO.Config.Config();

            objError = new BLL_Config().LoadInfo(ref objConfig);
            if (objError.IsError)
            {
                AppFormat.ShowMessage.ShowMsg(objError.MessageError);
                return;
            }

            if (objConfig == null || objConfig.s_ID.Trim().Length == 0) return;
            
            txtCostCreateAuction.Text = string.Format("{0:#,##0}", objConfig.CostCreateAuction);
            txtCostMultiTypeProduct.Text = string.Format("{0:#,##0}", objConfig.CostMultiTypeProduct);
            txtIncreaseValue.Text = string.Format("{0:#,##0}", objConfig.IncreaseValue);
            txtCost.Text = string.Format("{0:#,##0}", objConfig.Cost);
            txtCostChange.Text = string.Format("{0:#,##0}", objConfig.CostChange);
            txtMinIncreaseValue.Text = string.Format("{0:#,##0}", objConfig.MinIncreaseValue);
            txtMaxIncreaseValue.Text = string.Format("{0:#,##0}", objConfig.MaxIncreaseValue);
            txtMinCostChange.Text = string.Format("{0:#,##0}", objConfig.MinCostChange);
            txtMaxCostChange.Text = string.Format("{0:#,##0}", objConfig.MaxCostChange);
            
        }

        private bool Update()
        {
            objError = new AppFormat.MyError();
            DTO.Config.Config objConfig = new DTO.Config.Config();
            
            objConfig.CostCreateAuction = Convert.ToDecimal(txtCostCreateAuction.Text);
            objConfig.CostMultiTypeProduct = Convert.ToDecimal(txtCostMultiTypeProduct.Text);
            objConfig.IncreaseValue = Convert.ToDecimal(txtIncreaseValue.Text);
            objConfig.Cost = Convert.ToDecimal(txtCost.Text);
            objConfig.CostChange = Convert.ToDecimal(txtCostChange.Text);
            objConfig.MinIncreaseValue = Convert.ToDecimal(txtMinIncreaseValue.Text);
            objConfig.MaxIncreaseValue = Convert.ToDecimal(txtMaxIncreaseValue.Text);
            objConfig.MinCostChange = Convert.ToDecimal(txtMinCostChange.Text);
            objConfig.MaxCostChange = Convert.ToDecimal(txtMaxCostChange.Text);
           
            new BLL_Config().Insert(objConfig);

            if (objError.IsError)
            {
                AppFormat.ShowMessage.ShowMsg(objError.MessageError);
                return false;
            }            
            Response.Write("<script>alert('Cập nhật cấu hình thành công!')</script>");
            
            LoadData();
            return true;
        }

        #endregion
        
    }
}